﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GM.Model.SubSonic;

namespace GM.Forms
{
    public partial class DeleteAssetForm : Form
    {
        private bool m_isValid;
        private gm_srodki_trwale m_asset;

        public DeleteAssetForm()
        {
            InitializeComponent();
        }

        public void Delete(gm_srodki_trwale asset)
        {
            if (asset == null)
                return;
            m_asset = asset;
            Show();
            BringToFront();
        }

        private void DeleteAssetForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void tbxDeleteReport_Validated(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(tbxDeleteReport.Text))
            {
                errorProvider.SetError(tbxDeleteReport, "Numer dokumentu nie może być pusty.");
                m_isValid = false;
            }
            else
            {
                errorProvider.SetError(tbxDeleteReport, string.Empty);
                m_isValid = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!m_isValid)
                return;
            m_asset.zlikwidowany = 1;
            m_asset.Save();
            ClearForm();
            Hide();
            DeleteRequested();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
            Hide();
        }

        private void ClearForm()
        {
            tbxDeleteReport.Text = string.Empty;
            errorProvider.Clear();
        }

        public event Action DeleteRequested = delegate { };
    }
}
